package com.zl.iot.docking.util;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


/**
 * @描述 设备三元组加密类
 * @author 曾泽亦
 */
public class HmacSHA1 {

    //加密算法
//    private static String encryptionAlgorithm = "HmacSHA1";
    private static String encryptionAlgorithm = "Hmacmd5";
//    private static String encryptionAlgorithm = "Hmacsha256";

    public static String hmacSha1(String src, String key) {
        try {
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("utf-8"), encryptionAlgorithm);
            Mac mac = Mac.getInstance(encryptionAlgorithm);
            mac.init(signingKey);
            byte[] rawHmac = mac.doFinal(src.getBytes("utf-8"));
            return Hex.encodeHexString(rawHmac);
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args){
        String str = "clientId99032817000004DEdeviceName99032817000004DEproductKeya1LxQXnzlx4";
        String deviceSecret = "fadee73a0c0b7dfa44a1c33c3f252d43";
        System.out.println(hmacSha1(str,deviceSecret));
    }
}
